﻿using System.Text.Json;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using TianlalaHSB.Models;
using TianlalaHSB.Utils;

namespace TianlalaHSB.Views;

/// <summary>
///     AccountBalanceListView.xaml 的交互逻辑
/// </summary>
public partial class AccountBalanceView : Page
{
    public AccountBalanceView()
    {
        InitializeComponent();
    }

    private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        e.Handled = !Number.IsInteger(e.Text);
    }

    private async void SubmitButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            var subAcctNoVal = subAcctNo.Text.Trim();
            if (string.IsNullOrEmpty(subAcctNoVal)) throw new Exception("资金账号不能为空");

            // 准备POST请求的数据 
            var postData = new
            {
                headReqSerialNo = HttpUtil.GetHeadReqSerialNo(),
                subAcctNo = subAcctNoVal
            };

            var responseBody = await HttpUtil.Request("03050103A1019", postData);
            var list = JsonSerializer.Deserialize<AccountBalance>(responseBody);
            if (list == null) throw new Exception("无数据");

            // 在这里处理获取到的数据，例如更新UI
            RecordListView.ItemsSource = new List<AccountBalance>
            {
                list
            };
        }
        catch (Exception ex)
        {
            // 处理异常，例如网络问题或无效的请求等
            MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}